/**
 * Created by charlie on 18-1-11.
 */
import { editSure } from '../services/editPwd'
import { Actions } from 'react-native-router-flux'
export default {
    namespace:'editPwd',
    state:{
        userName:'',
        newPwdSure:'',
        newPwd:'',
        oldPwd:'',
        dialogText:'',
      unresolved:''
    },
    subscriptions:{

    },
    effects:{
        *getInfoes({payload},{call,put,select}){
            const {userName} = yield select(state => state.login);
            yield put({type:'basicInfoes',payload:userName})
        },
        *change({payload},{put,select,call}){
            yield put({type: 'changeSuccess',payload: payload})
        },
        *edit({payload},{put,select,call}){
            const {newPwdSure,newPwd,oldPwd} = yield select(state => state.editPwd);
            const {userId} = yield select(state => state.login);
            if(newPwdSure != newPwd){
                yield put({type:'thisDialog/thisInit'});
                yield put({type:'dialogShow',payload:'两次输入的新密码不同，请重新输入！'});
            }else {
                const { json, error } = yield call(editSure, {userId: userId, oldPassword: oldPwd,newPassword: newPwd});

                if(error || json.code === '0'){
                    yield put({type:'thisDialog/thisInit'});
                    yield put({type:'dialogShow',payload:'修改失败！'});
                }else{
                    yield put({type:'goLogin'})
                }
            }
        },
    },

    reducers:{
        basicInfoes(state, {payload}){
            return { ...state, userName:payload}
        },
        changeSuccess(state, {payload}){
            return { ...state, ...payload}
        },
        goLogin(state,{payload}){
            Actions.push('Login');
            return{...state,oldPwd:'',newPwd:'',newPwdSure:''}
        },
        getClean(state,{payload}){
            return {...state,oldPwd:'',newPwd:'',newPwdSure:'',dialogText:''}
        },
        dialogShow(state,{payload}){
            return {...state,dialogText:payload}
        }
    }
}